/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"distribute-mapreduce/pkg/coordinator"
	"github.com/sirupsen/logrus"
	"os"
	"strconv"

	"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use:   "distribute-mapreduce-coordinator",
	Short: "coordinator",
	// Uncomment the following line if your bare application
	// has an action associated with it:
	Run: func(cmd *cobra.Command, args []string) {
		portString := cmd.Flag("port").Value.String()
		portInt32, err := strconv.ParseInt(portString, 10, 32)
		if err != nil {
			logrus.Fatalf("port parse error: %v", err)
		}
		if len(args) == 0 {
			logrus.Warnf("no input files")
		}
		c := coordinator.NewCoordinator(args, 10, int(portInt32))
		logrus.Infof("[coordinator]-runing on port %d", portInt32)
		c.Run() // block until exit
	},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	err := rootCmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}

func init() {
	// Here you will define your flags and configuration settings.
	// Cobra supports persistent flags, which, if defined here,
	// will be global for your application.

	// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.distribute-mapreduce.yaml)")

	// Cobra also supports local flags, which will only run
	// when this action is called directly.
	//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
	//rootCmd.Args
	rootCmd.Flags().Int32P("port", "p", 8360, "port")
}
